/**
 * 消息提醒记录
 */
// var allListFlag = true;
var tzList = {};
var searchFlag = true;

$(function () {
    signsy();//初始化加载列表信息

    /*
     *查询 检查室信息
     */
    $("#queryMenu").click(function () {
        signsy();
    });


    // $("#editModelDepartment").click(function () {
    //     $.ajax({
    //         // url : "data/checkroomManagement.json",
    //         // url: "/sys/dept/info",
    //         url: "http://192.168.2.106:8080/sys/dept/info",
    //         userType: "get",
    //         async: false,
    //         dataType: "json",
    //         data: {},
    //         success: function (data) {
    //             var nothtml = "";
    //             if (data.code == 1 && data.data.length) {
    //                 for (var i = 0; i < data.data.length; i++) {
    //                     nothtml += "<option userId ='" + data.data[i].departmentId + "'>" + data.data[i].departmentName + "</option>";
    //                 }
    //                 $("#editModelDepartment").html(nothtml);
    //             }
    //         }
    //     });
    function getDeptInfo() {
        $.ajax({
            // url : "data/checkroomManagement.json",
            url: "/sys/dept/info",
            // url: "http://192.168.2.106:8080/sys/dept/info",
            type: "get",
            async: false,
            dataType: "json",
            data: {},
            success: function (data) {
                var nothtml = "";
                if (data.code == 1 && data.data.length) {
                    for (var i = 0; i < data.data.length; i++) {
                        nothtml += "<option userId ='" + data.data[i].departmentId + "'>" + data.data[i].departmentName + "</option>";
                    }
                    $("#editModelDepartment").html(nothtml);
                }
            }
        });
    }

    function getDeptInfo2() {
        $.ajax({
            // url : "data/checkroomManagement.json",
            url: "/sys/dept/info",
            // url: "http://192.168.2.106:8080/sys/dept/info",
            type: "get",
            async: false,
            dataType: "json",
            data: {},
            success: function (data) {
                var nothtml = "";
                if (data.code == 1 && data.data.length) {
                    for (var i = 0; i < data.data.length; i++) {
                        nothtml += "<option userId ='" + data.data[i].departmentId + "'>" + data.data[i].departmentName + "</option>";
                    }
                    $("#addDeptId").html(nothtml);
                }
            }
        });
    }

    $("#addCheckRoom").click(function () {
        //清空模态框
        var modal = $("#myModal_add");  //当前模态框
        var modalTag = modal.find('.form-control');
        for (var i = 0; i < modalTag.length; i++) {
            if(i != 2){
                modalTag.eq(i).val("");
            }

        }
        getDeptInfo2();
    })

    //新增
    $("#submitMenu_add").click(function () {
        // 点击提交再次验证
        if (!($("#addCheckId").val().trim())) {
            $('.add-checkId-err').removeClass('hide').find("em").text('检查室ID不能为空');
            $('.add-checkId-err').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        } else {
            $('.add-checkId-err').addClass("hide");
        }
        if (!($("#addCheckName").val().trim())) {
            $('.add-checkName-err').removeClass('hide').find("em").text('检查室名称不能为空');
            $('.add-checkName-err').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        } else {
            $('.add-checkName-err').addClass("hide");
        }
        if (!($("#addQueryPreFix").val().trim())) {
            $('.add-preFix-err').removeClass('hide').find("em").text('检查室排队号前缀不能为空');
            $('.add-preFix-err').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        } else {
            $('.add-preFix-err').addClass("hide");
        }

        var checkRoomId = $("#addCheckId").val().trim();
        var departmentId = $("#addDeptId").find("option:selected").attr("id");
        var checkRoomName = $("#addCheckName").val().trim();
        var checkRoomDisplayName = $("#addDisplayName").val().trim();
        var queuePrefix = $("#addQueryPreFix").val().trim();

        $.ajax({
            url: "/sys/checkRoom/add",
            // url: "http://192.168.2.106:8080/sys/checkRoom/add",
            type: "post",
            async: false,
            dataType: "json",
            data: {
                checkRoomId: checkRoomId,
                departmentId: departmentId,
                checkRoomName:checkRoomName,
                checkRoomDisplayName: checkRoomDisplayName,
                queuePrefix: queuePrefix,
                // checkIntroduction: checkIntroduction
            },
            success: function (data) {
                if (data.code == 1 && data.msg != null) {
                    $("#myModal_add").modal("hide");
                    signsy();
                    // alert("新增成功！");
                }
                else{
                    alert("新增失败！"+data.msg);
                }

            }, error: function (data) {
                console.log(data.msg);
                alert("新增失败！"+data.msg);
            }
        });

    });

    //修改
    $("#submitMenu_update").click(function () {
        // 点击提交再次验证
        if (!($("#editModelName").val().trim())) {
            $('.change-deptName-err').removeClass('hide').find("em").text('检查室名称不能为空');
            $('.change-deptName-err').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        } else {
            $('.change-deptName-err').addClass("hide");
        }
        if (!($("#editModelQueryPreFix").val().trim())) {
            $('.change-preFix-err').removeClass('hide').find("em").text('检查室排队号前缀不能为空');
            $('.change-preFix-err').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        } else {
            $('.change-preFix-err').addClass("hide");
        }
        /*
                var editModelName = $("#editModelName").val().trim();
                var editModelDepartment = $("#editModelDepartment").val().trim();
        */
        var editModelDisplayName = $("#editModelDisplayName").val().trim();
        var editModelQueryPreFix = $("#editModelQueryPreFix").val().trim();
        var departmentId = $("#editModelDepartment").find("option:selected").attr("id");
        var checkroomId = $("#submitMenu_update").attr("data-check");
        var checkRoomName = $("#editModelName").val().trim();
        // var checkIntroduction = $("#checkIntroduction").val();
        $.ajax({
            url: "/sys/checkRoom/update",
            // url: "http://192.168.2.106:8080/sys/checkRoom/update",
            type: "post",
            async: false,
            dataType: "json",
            data: {
                checkRoomId: checkroomId,
                departmentId: departmentId,
                checkRoomName:checkRoomName,
                checkRoomDisplayName: editModelDisplayName,
                queuePrefix: editModelQueryPreFix,
                // checkIntroduction: checkIntroduction
            },
            success: function (data) {
                if (data.code == 1 && data.msg != null) {
                    $("#myModal_update").modal("hide");
                    signsy();
                }
                else{
                    alert("修改失败！"+data.msg);
                }

            }, error: function (data) {
                alert("修改失败！"+data.msg);
            }
        });
    });

    /*
     *查询 通知方式信息
     */
    function signsy(page, pageSize, pageNo) {
        //得到查询条件
        var checkRoomName = $("#checkRoomName").val();
        var checkRoomDisPlayName = $("#checkRoomDisPlayName").val().trim();
        /*
         *方式列表
         */
        // if(allListFlag){
        $.ajax({
            // url : "data/checkroomManagement.json",
            url: "/sys/checkRoom/info",
            // url: "http://localhost:8080/sys/checkRoom/info",
            // url: "http://192.168.2.106:8080/sys/checkRoom/info",
            type: "get",
            async: false,
            dataType: "json",
            data: {
                roomName: checkRoomName,
                roomDisplayName: checkRoomDisPlayName
            },
            success: function (data) {
                var nothtml = "<tr><th class='index'>序号</th><th  class='checkRoomName'>检查室名</th><th class='departmentName'>所属科室</th>" +
                    "<th class='checkRoomDisplayName'>显示名称</th><th class='queuePrefix'>排队序号前缀</th><!--<th class='checkIntroduction'>检查室简介</th>--><th class='op'>操作</th></tr>";
                if (data.code == 1 && data.data.length) {
                    for (var i = 0; i < data.data.length; i++) {
                        nothtml += "<tr><td>" + (i + 1) + "</td>" +
                            "<td>" + data.data[i].checkRoomName + "</td><td>" + data.data[i].departmentName + "</td><td>" + data.data[i].checkRoomDisplayName + "</td><td>" + data.data[i].queuePrefix + "</td>" +
                            /*"<td>"+data.data[i].checkIntroduction+"</td>" +*/
                            "<td><a href='#' data-toggle='modal' data-target='#myModal_update' data-roomId='" + data.data[i].checkRoomId + "' userId='" + data.data[i].departmentId + "' class='btn btn-sm btn-primary operation allUpdate'>修改</a>" + "<a href=\"#\" data-toggle=\"modal\" userId='" + data.data[i].checkRoomId + "' " +
                            "class=\"btn btn-sm btn-danger operation allDele\">删除</a></td></tr>";
                    }
                    $("#TeamSignedUser").html(nothtml);

                    /*
                     *填充 修改模态框的数据
                     */
                    $(".allUpdate").click(function () {
                        // 点击修改清空模态框的标志
                        $('.change-displayName-err').removeClass('hide').find("em").text('');
                        $('.change-displayName-err').find("i").removeAttr('class', 'icon-ok');
                        $('.change-displayName-err').find("i").removeAttr('class', 'icon-warn');
                        // 点击修改清空模态框的标志
                        $('.change-preFix-err').removeClass('hide').find("em").text('');
                        $('.change-preFix-err').find("i").removeAttr('class', 'icon-ok');
                        $('.change-preFix-err').find("i").removeAttr('class', 'icon-warn');

                        // 填充数据
                        var tag = $(this).closest('tr').find('td');
                        var modal = $("#myModal_update");  //当前模态框
                        var modalTag = modal.find('.form-control');
                        modal.find('#submitMenu_update').attr("data-userId", this.id);
                        modal.find('#submitMenu_update').attr("data-check", $(this).attr("data-roomId"));

                        for (var i = 1, j = 0; i < tag.length; i++, j++) {
                            modalTag.eq(j).val(tag.eq(i).text());
                        }
                        var deptName = tag.eq(2).text();
                        //获取科室信息
                        getDeptInfo();
                        //选中
                        var list = $("#editModelDepartment option").get();
                        for(var i = 0;i < list.length;i++){
                            if(list[i].innerHTML == deptName){
                                $(list[i]).prop("selected","true");
                            }
                        }

                        // $("#departmentIntroduction").val(temp);
                    });
                    $(".allDele").click(function () {
                        var pid = this.id;
                        if (confirm("确认删除当前项？")) {
                            $.ajax({
                                url: "/sys/checkRoom/delete",
                                // url: "http://192.168.2.106:8080/sys/checkRoom/delete",
                                type: "post",
                                async: false,
                                dataType: "json",
                                data: {
                                    roomId: pid
                                },
                                success: function (data) {
                                    if (data.code == 1 && data.msg != null) {
                                        $(this).parents("td").parents("tr").remove();
                                        $("#total").html($("#total").html() * 1 - 1);
                                        $("#queryMenu").click();
                                    } else {
                                        alert("删除失败！");
                                    }
                                }, error: function () {
                                    alert("请求超时！");
                                }
                            });
                        }
                    });

                } else {
                    nothtml += "<tr><td colspan='6'>数据为空！！</td></tr>";
                    $("#TeamSignedUser").html(nothtml);
                    $("#total").html(0);
                    $("#current").html(0);
                }
            }, error: function () {
                alert("请求超时！");
            }
        });
        // }
        /* if(searchFlag){
             $.ajax( {
                 url : "lo_messageALert.do",
                 userType : "post",
                 async : false,
                 dataType : "json",
                 data : {
                     op:page,
                     pageSize:pageSize,
                     pageNo:pageNo,
                     notificationID: remindingRecordNotificationID,
                     alertNumber: remindingRecordAlertNumber,
                     startTime: SystemLogIDTimeStartNew,
                     endTime: SystemLogIDTimeEndNew,
                     alertState: remindingRecordAlertState
                 },
                 success : function(data) {
                     var nothtml = "<tr><th class='index'>序号</th><th>信息通知方式</th><th>信息内容</th><th class='time'>提醒时间</th><th>提醒对象</th><th class='state'>状态</th></tr>";
                     if(data.statemt==1 && data.data!=null){
                         for(var i=0;i<data.data.length;i++){
                             var state = data.data[i].alertstate;
                             //0成功/1失败
                             if( state==0 ){
                                 state="成功";
                             }else if(state == 1){
                                 state="失败";
                             }else{
                                 state = "";
                             }
                             nothtml+="<tr><td>"+(i+10*(data.currPage-1)+1)+"</td>" +
                                 "<td>"+tzList[data.data[i].notificationid]+"</td><td>"+data.data[i].messagecontent+"</td><td>"+getTime(data.data[i].alertdatetime)+"</td><td>" +data.data[i].alertnumber + "</td>" +
                                 "<td>" + state + "</tr>";
                         }
                         $("#TeamSignedUser").html(nothtml);

                         //赋值
                         $("#total").html(data.totalCount);
                         $("#current").html(data.currPage);

                     }else
                     {
                         nothtml+="<tr><td colspan='6'>数据为空！</td></tr>";
                         $("#TeamSignedUser").html(nothtml);
                         $("#total").html(0);
                         $("#current").html(0);
                     }
                 }
             });
         }*/
    }


})
